package shopping.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import shopping.dto.MenuDto;
import shopping.entity.Menu;
import shopping.entity.ParentEntity;
import shopping.service.interfaces.IMenuService;
import shopping.util.ResultCode;
import shopping.util.ResultData;

/**
 * 菜单
 * 
 * @author JiangHuiJun
 *
 */
@Controller
@RequestMapping("menu")
public class MenuController {

	private static final Logger log = LoggerFactory.getLogger(MenuController.class);

	@Resource
	private IMenuService menuServiceImpl;

	/**
	 * 分页查询
	 * 
	 * @return
	 */
	@RequestMapping("queryPage.action")
	@ResponseBody
	public ResultData queryPage(@RequestBody MenuDto menu) {
		ResultData resultData = new ResultData();
		try {
			Map<String, Object> content = new HashMap<String, Object>();
			if(null !=menu.getCode() && !"".equals(menu.getCode()))
				content.put("code", menu.getCode());
			if(null !=menu.getUrl() && !"".equals(menu.getUrl()))
				content.put("url", menu.getUrl());
			if(null !=menu.getName() && !"".equals(menu.getName()))
				content.put("name", menu.getName());
			List<Menu> list = menuServiceImpl.findByHqlPage(content, menu.getFirstResult()-1, menu.getMaxResult());
			int count = menuServiceImpl.countByHql(content);
			resultData.setData(list);
			resultData.setCount(count);
		} catch (Exception e) {
			resultData.parse(ResultCode.SYSTEM_ERROR);
			log.info(e.getMessage());
		}
		return resultData;
	}
	
	/**
	 * 更新
	 * @return
	 */
	@RequestMapping(value = "/update.action")
	@ResponseBody
	public ResultData Update(@RequestParam String param){
		ResultData resultData = new ResultData();
		try {
			Menu tObject = JSON.parseObject(param,Menu.class);
			menuServiceImpl.update(tObject);
		} catch (Exception e) {
			
			resultData.parse(ResultCode.SYSTEM_ERROR);
		}
		return resultData;
	}

}
